#include<iostream>
#include<unistd.h>
#include<signal.h>
void Printpending(sigset_t pending)
{
    printf("我是一个进程(%d),pending: ",getpid());
    for(int i = 32; i >= 1; i--)
    {
        if(sigismember(&pending,i))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}
void handlersig(int sig)
{
    std::cout << "###############################" << std::endl;
    std::cout << "sig : " << sig << std::endl;
    signal(sig , SIG_DFL);
    sigset_t pending;//pending在抵达前会先被置0
    sigpending(&pending);
    Printpending(pending);
    std::cout << "信号恢复成功" << std::endl;
    std::cout << "###############################" << std::endl;
}
int main()
{
    sigset_t block,oblock;
    signal(2,handlersig);
    sigemptyset(&block);
    sigemptyset(&oblock);
    sigaddset(&block,2);
    int n = sigprocmask(SIG_SETMASK,&block,&oblock);
    int cnt = 5;
    
    
    while(true)
    {
        sigset_t pending;
        sigpending(&pending);
        Printpending(pending);
        if(sigismember(&pending,2) && cnt >= 0)
        {
            cnt--;
        }
        if(cnt < 0)
        {
            //解除屏蔽
            sigprocmask(SIG_SETMASK,&oblock,&block);
        }
        sleep(1);
    }
    return 0;
}